import {
  SVAEUSERINFO,
  DELETE_USER_INFO
} from './constant'

const token = localStorage.getItem('token')
const user = JSON.parse(localStorage.getItem('user'))

const defaultState = {
  user: user || {},
  token: token || '',
  isLogin: token && user ? true : false
}

export default function loginReducer(state = defaultState, action) {
  switch (action.type) {
    case SVAEUSERINFO:
      return {
        ...state, user: action.userInfo.user, token: action.userInfo.token, isLogin: true
      }
      case DELETE_USER_INFO:
        return {
          ...state,
          user: {},
            token: '',
            isLogin: false
        }
        default:
          return state
  }
}